<!DOCTYPE html>
<html lang="en">
<title>新增渠道用户</title> <#import "/layout/layout.html" as body/>
<@body.page>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta charset="UTF-8"> 
<div class="row wrapper border-bottom white-bg page-heading">
	<div class="col-lg-10">
		<h2>运营管理</h2>
		<ol class="breadcrumb">
			<li><a href="../store_list.html">运营管理</a></li>
			<li class="active"><strong>新增渠道用户</strong></li>
		</ol>
	</div>
</div>
<div class="wrapper wrapper-content animated fadeInRight">
	<div class="row">
		<div class="col-lg-12">
			<div class="ibox float-e-margins">
				<div class="ibox-title">
					<h5 class="pull-left">新增渠道用户</h5>
				</div>
				<div class="ibox-content">
					<form class="form-horizontal" method="post" action="channer_and" id="from" accept-charset="UTF-8">
						<div class="form-group">
							<label class="col-sm-2 control-label">渠道用户名称</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="channelName" name="channelName" placeholder="添加渠道名称，用于自动回复时显示">
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">子渠道名称</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="channelChild" name="channelChild" placeholder="添加该二维码使用者的名称">
							</div>
						</div>
						<div class="hr-line-dashed"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">手机号码</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="mobile" name="mobile" placeholder="必填，请输入该用户手机号码">
							</div>
						</div>
						<div class="form-group">
                        	<input type="hidden" id="couponId" name ="couponId">
                            <label class="col-sm-2 control-label">优惠券ID</label>
                            <div class="col-sm-10" id="">
                            	<input type="number" id="CouponIdChild" name="CouponIdChild" class="form-control" placeholder="非必填，如有填写，则用户关注该渠道二维码后，会发送所填写的对应优惠券">
                                <div id="add_list"></div>
                                <a href="javascript:;" class="pull-right" style="padding-top: 20px;color: #666;" id="add_input">再添加一条优惠券ID</a>
                            </div>
                        </div>
						<div class="hr-line-dashed"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">单用户返佣</label>
							<div class="col-sm-10">
								<input type="number" class="form-control" id="commision" name="commision"  placeholder="非必填，如有则填写，系统将结合带量帮您计算须返佣总数">
							</div>
						</div>
						<div class="hr-line-dashed"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">其他备注</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="remark" name="remark" placeholder="非必填">
							</div>
						</div>
						<!-- 这里填写二维码地址 -->
						<!-- <div class="form-group">
							<a download><div id="qrcode" class="col-sm-10" download></div></a>
							<input type="hidden" id="getval"/>
					    </div> -->
					    <input type="hidden" name="id" value="0" id="id">
						<div class="hr-line-dashed"></div>
					</form>
				</div>
				<div class="ibox-content overflow">
					<div class="col-sm-12">
						<button type="button" class="btn btn-default pull-left" id="btn_cancel">取消添加</button>
						<button type="button" id="send" class="btn btn-primary pull-right" >新增并生成二维码</button>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<script src="${baseUrl}js/main.js"></script>
<!-- <script src="${baseUrl}js/channel/qrcode.js"></script> -->
<script>
	$(document).ready(function() {
		$('#add_input').click(function () {
            var list = '<div class="add-list overflow"><div class="col-sm-11" style="padding: 0 15px 0 0;"><input type="number" class="form-control" placeholder="必填，请输入优惠券ID"></div><div class="col-sm-1" style="padding: 2px 15px 0 0;"><button type="button" class="btn btn-default btn-sm" onclick="delete_list(this)">删除</button></div></div>';
            $('#add_list').append(list);
        });
		
		$("#send").click(function(){
			var msgs = msg();
			if(msgs.length > 0 ){
				/* alert(msgs); */
				swal({
		            title: "错误提示",
		            html: msgs,
		            type: "error"
		        });
				return false;
			}
			
			var couponId = $("#CouponIdChild").val();
			$("#add_list input[type='number']").each(function() {
				var msg = $(this).val();
				if(msg.length > 0 && couponId.length == 0){
					couponId = msg;
				}else if(msg.length > 0 && couponId.length > 0){
					couponId = couponId +"#"+ msg;
				}
			});
			$("#couponId").val(couponId);
			$.ajax({
	             url: "channer_save",  // 请求地址
	             data:$('#from').serialize(),// 你的formid,
	             type: 'post',
	             cache: false,
	             dataType: 'json',
	             success: function (datas) {
	            	 var data = datas.data;
	            	 if(datas.errorCode == -1){
	            		 swal("保存失败", "您的保存操作失败请联系管理员", "error");
	            		 return ;
	            	 }else{
	            		 swal({
	            	    		title: '保存成功！',
	            	            text: "点击'确定'为您跳转查询列表，点击'继续'停留此页刷新当前页",
	            	            /* imageUrl:"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+channerQrcodeR.ticket, */
	            	            type: 'question',
	            	            showCancelButton: true,
	            	            cancelButtonColor: '#d33',
	            	            confirmButtonText: '确定',
	            	            cancelButtonText: '继续'
	            	    		}).then(function () {
	            	    			window.location.href = "to_channer_list";
	            	    		}, function (dismiss) {
	            	    		  if (dismiss === 'cancel') {
	            	    			  history.go(0); 
	            	    		  }
	            	    		});
	            	 }
	            	 
	             },
	             error: function () {
	            	 swal("保存失败", "您的保存操作失败请联系管理员", "error")
	             }
	         });
			 /* $("#from").submit(); */
		});
		function msg(){
			var msgs  = "";
			var channelName = $("#channelName").val();
			if(channelName.length == 0){
			  	msgs =msgs+ "渠道名不能为"+"</br>";
			}
			var channelChild = $("#channelChild").val();
			if(channelChild.length == 0){
			  	msgs =msgs+ "子渠道名不能为空"+"</br>";
			}
			var mobile = $("#mobile").val();
			if(mobile.length == 0){
			  	msgs = msgs+"手机号码不能为空"+"</br>";
			}
			if(!(/^1[34578]\d{9}$/.test(mobile))){
				msgs = msgs+"手机号码不正确"+"</br>";
			}
			var commision = $("#commision").val();
			if(commision != null && commision != ""){
				msgs =msgs+ (parseInt(commision)<0?"佣金不能小于0"+"</br>":"");
			}
			return msgs;
		}
		function check_code() {
			alert("ss");
	        /* swal({
	            title: "二维码生成成功！",
	            html: "<img src='../img/a6.jpg'><p style='margin-top: 10px'>点击下方按钮下载，或在列表页下载. </p>",
	            type: "success"
	        }); */
	    }
		
		// 点击取消按钮
		$("#btn_cancel").click(function(){
			swal({
	    		title: '取消保存？',
	            text: "点击确定跳转查询列表页，点击刷新，刷新当前页面",
	            type: 'question',
	            showCancelButton: true,
	            cancelButtonColor: '#d33',
	            confirmButtonText: '确定',
	            cancelButtonText: '刷新'
	    		}).then(function () {
	    			window.location.href = "to_channer_list";
	    		}, function (dismiss) {
	    		  if (dismiss === 'cancel') {
	    			  history.go(0); 
	    		  }
	    		});
		});
		
	});
	function delete_list(obj) {
        $(obj).parents('.add-list').remove();
    }
</script>
<script>
   /*  window.onload =function(){
        var qrcode = new QRCode(document.getElementById("qrcode"), {
            width : 96,//设置宽高
            height : 96
        });
        qrcode.makeCode("http://www.baidu.com");
        document.getElementById("send").onclick =function(){
            qrcode.makeCode(document.getElementById("getval").value);
            var url = document.getElementById("getval").value;
        }
    } */
    
</script>
</@body.page>
</html>